package com.fnfn.sdk.api.service.impl;

import java.util.Date;
import java.util.List;

import com.fnfn.sdk.api.common.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fnfn.sdk.api.common.DateUtil;
import com.fnfn.sdk.api.dao.PermissionMapper;
import com.fnfn.sdk.api.entity.Permission;
import com.fnfn.sdk.api.service.IPermissionService;

@Service("iPermissionService")
public class PermissionServiceImpl implements IPermissionService {
    private final PermissionMapper permissionMapper;

    @Autowired
    public PermissionServiceImpl(PermissionMapper permissionMapper) {
        this.permissionMapper = permissionMapper;
    }

    @Override
    public Permission selectByPermissionName(String permissionName) throws Exception {
        return permissionMapper.selectByPermissionName(permissionName);
    }

    @Override
    public Permission selectByPermissionType(String permissionType) throws Exception {
        return permissionMapper.selectByPermissionType(permissionType);
    }

    @Override
    public void add(Permission permission) throws Exception {
        permission.setCreateTime(new Date());
        permission.setState(Constant.STATE_EFFECTIVE);
        permissionMapper.insertSelective(permission);
    }

    @Override
    public void remove(Integer cPermissionId) throws Exception {
        permissionMapper.removeByPermissionId(cPermissionId);
    }

    @Override
    public void modify(Permission permission) throws Exception {
        permission.setUpdateTime(new Date());
        permissionMapper.updateByPrimaryKeySelective(permission);
    }

    @Override
    public List<Permission> selectByCondition(Permission permission) throws Exception {
        if (StringUtils.isNotEmpty(permission.getEndDate())) {
            permission.setEndDate(DateUtil.addDate(permission.getEndDate(), 1));
        }
        return permissionMapper.selectByCondition(permission);
    }

    // 修改
    @Override
    public List<Permission> selectByConditionRole(Permission permission) throws Exception {
        if (StringUtils.isNotEmpty(permission.getEndDate())) {
            permission.setEndDate(DateUtil.addDate(permission.getEndDate(), 1));
        }
        return permissionMapper.selectByConditionRole(permission);
    }
}
